package com.jay.lang.management.clazz;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.rmi.NotBoundException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.rmi.RMIConnector;
import javax.management.remote.rmi.RMIServer;

public class ManagementFactoryCls {
	public static void main(String[] args) throws IOException,
			NotBoundException {
		// 各种取Bean的静态方法，见各接口示例

		// jmx获取MBeanServerConnection的方法1
		JMXServiceURL url = new JMXServiceURL(
				"service:jmx:rmi:///jndi/rmi://10.1.60.14:6666/osgi");
		JMXConnector jmxConnector = JMXConnectorFactory.connect(url);
		MBeanServerConnection mbsc = jmxConnector.getMBeanServerConnection();

		// jmx获取MBeanServerConnection的方法2
		Registry registry = LocateRegistry.getRegistry("10.1.60.14", 6666);
		RMIServer stub = null;
		JMXConnector jmxc = null;
		if (stub == null) {
			stub = (RMIServer) registry.lookup("osgi");
		}
		jmxc = new RMIConnector(stub, null);
		jmxc.connect();
		MBeanServerConnection mbsc1 = jmxc.getMBeanServerConnection();

		// 代理方法是取得MBean的第三种方法，详见jdk doc
		RuntimeMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(mbsc,
				ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);

	}
}
